package com.firestone.service.center.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.firestone.bean.dto.role.RoleDTO;
import com.firestone.bean.entity.mysql.primary.ImCenterManagerRRole;
import com.firestone.bean.entity.mysql.primary.ImCenterRole;
import com.firestone.bean.entity.mysql.primary.ImCenterRoleRMenu;
import com.firestone.core.enums.SystemCodeEnum;
import com.firestone.core.exception.FireException;
import com.firestone.dao.primary.center.ImCenterRoleDao;
import com.firestone.service.center.IImCenterManagerRRoleService;
import com.firestone.service.center.IImCenterRoleRMenuService;
import com.firestone.service.center.IImCenterRoleService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author CAIYJ
 * @since 2018-03-27
 */
@Service
public class ImCenterRoleServiceImpl
        extends ServiceImpl<ImCenterRoleDao, ImCenterRole>
        implements IImCenterRoleService {

    @Autowired
    private IImCenterRoleRMenuService imCenterRoleRMenuService;

    @Autowired
    private IImCenterManagerRRoleService imCenterManagerRRoleService;

    @Override
    public Page<RoleDTO> listRole(Page<RoleDTO> page, String name)
            throws Exception {
        EntityWrapper<RoleDTO> ew = new EntityWrapper<>();
        ew.setEntity(new RoleDTO());
        ew.where(StringUtils.isNotBlank(name),
                "cr.`name` LIKE concat('%',{0},'%')", name);
        List<RoleDTO> list = this.baseMapper.selectRoleList(page, ew);
        return page.setRecords(list);
    }

    @Override
    @Transactional
    public boolean saveRole(ImCenterRole role, Long[] menuIds)
            throws Exception {
        boolean result = this.insert(role);
        if (menuIds != null && menuIds.length > 0) {
            List<ImCenterRoleRMenu> list = new ArrayList<>();
            ImCenterRoleRMenu centerRoleRMenu = null;
            for (Long menuId : menuIds) {
                centerRoleRMenu = new ImCenterRoleRMenu();
                centerRoleRMenu.setRoleId(role.getId());
                centerRoleRMenu.setMenuId(menuId);
                list.add(centerRoleRMenu);
            }
            result = imCenterRoleRMenuService.insertBatch(list);
        }
        return result;
    }

    @Override
    @Transactional
    public boolean updateRole(ImCenterRole role, Long[] menuIds)
            throws Exception {
        boolean result = this.updateById(role);
        EntityWrapper<ImCenterRoleRMenu> ew = new EntityWrapper<>();
        ew.where("role_id = '{0}'", role.getId());
        imCenterRoleRMenuService.delete(ew);
        if (menuIds != null && menuIds.length > 0) {
            List<ImCenterRoleRMenu> list = new ArrayList<>();
            ImCenterRoleRMenu centerRoleRMenu = null;
            for (Long menuId : menuIds) {
                centerRoleRMenu = new ImCenterRoleRMenu();
                centerRoleRMenu.setRoleId(role.getId());
                centerRoleRMenu.setMenuId(menuId);
                list.add(centerRoleRMenu);
            }
            result = imCenterRoleRMenuService.insertBatch(list);
        }
        return result;
    }

    @Override
    public boolean removeRole(Long roleId) throws Exception {
        boolean result = false;
        if (roleId != null) {
            EntityWrapper<ImCenterManagerRRole> ew = new EntityWrapper<>();
            ew.where("role_id = '{0}'", roleId);
            int count = imCenterManagerRRoleService.selectCount(ew);
            if (count > 0) {
                throw new FireException(
                        SystemCodeEnum.error_delete_failed.getErrorCode(),
                        "存在拥有该角色的账户，不能删除", null);
            }
            EntityWrapper<ImCenterRoleRMenu> rew = new EntityWrapper<>();
            ew.where("role_id = '{0}'", roleId);
            result = imCenterRoleRMenuService.delete(rew);
        }
        return result;
    }

}
